package com.jinandi.app.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.jinandi.app.domain.User;

public interface UserDao extends PagingAndSortingRepository<User, Integer>, JpaSpecificationExecutor<User> {

	// 原生查询
	@Query(value = "SELECT * FROM jnd_user", nativeQuery = true)
	List<User> findAllUser();

	// 声明查询 ?1 ?2 数字可以省略
	@Query("select u from User u where u.name = ?1 and u.password =?2")
	User findByName(String name, String password);

	// 使用命名参数
	@Query("select u from User u where u.name = :name and u.password = :password")
	User findByName2(@Param("name") String name,
			@Param("password") String password);

	/**
	 * <修改密码>
	 * @param password
	 * @param name
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:41:47
	 */
	@Modifying
	@Transactional
	@Query("update User u set u.password = ?1 where u.name = ?2")
	int modifyPassword(String password, String name);

	/**
	 * <根据用户名和密码查询>
	 * @param name
	 * @param password
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:41:06
	 */
	List<User> findByNameAndPassword(String name, String password);

	/**
	 * <登录方法>
	 * @param username
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:40:45
	 */
	@Query("from User where enabled = true and username = ?1")
	List<User> loadUserByUsername(String username);
	
	/**
	 * <根据ID查询用户>
	 * @param id
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:40:23
	 */
	User findUserById(Long id);
	
	/**
	 * <根据ID删除用户>
	 * @param id
	 * @author CGC 
	 * 创建日期:2013-5-9 下午1:39:53
	 */
	@Modifying
	@Transactional
	@Query("delete from User where id = ?1")
	void deleteUserById(Long id);
}